package com.horoscope.astrology.zodiac.signs.free.fragment

import android.app.Activity
import android.content.Context
import android.content.Context.INPUT_METHOD_SERVICE
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.utils.MyAnimationUtils
import com.horoscope.astrology.zodiac.signs.free.utils.ToastUtil

open class BaseFragment : Fragment() {

    protected var mSwipeRefreshLayout2: SwipeRefreshLayout? = null
    protected var mRoot: View? = null
    protected var mNameStr = ""
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }

    protected lateinit var activity: Activity


    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.activity = context as Activity
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden){
            ToastUtil.cancel()
        }
    }

    fun fadeInRoot(mRoot: View?) {
        if (mRoot == null) return
        mRoot.postDelayed({
            val anim = AnimationUtils.loadAnimation(activity, R.anim.fade_in)
            mRoot.startAnimation(anim)
        }, 100)

    }

    fun inVisibilityRoot() {
        mRoot?.visibility = View.INVISIBLE
    }

    protected fun showInput(et: EditText) {
        et.requestFocus()
        val imm = activity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
    }

    /**
     * 隐藏键盘
     */
    protected fun hideInput() {
        val imm = activity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        val v = activity.window?.peekDecorView()
        if (null != v) {
            imm.hideSoftInputFromWindow(v.windowToken, 0)
        }
    }


    fun showLoading(view1: ImageView, view2: View) {
        view1.visibility = View.VISIBLE
        view2.visibility = View.VISIBLE
        view1.startAnimation(MyAnimationUtils.loadingAnimation())
    }

    fun showLoaded(view1: ImageView?, bg: View?) {
        if (isAdded) {
            bg?.visibility = View.INVISIBLE
            view1?.clearAnimation()
            view1?.visibility = View.INVISIBLE

        }
    }

}